﻿// -----------------------------------------------------------------------
// <copyright file="SqlServerConnectionStringBuilder.cs" company="JSC Centido">
//  Copyright (c) JSC Centido. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace Centido.Core
{
    using System;


    /// <summary>
    ///   Represents Sql Server connection string builder
    /// </summary>
    public sealed class SqlServerConnectionStringBuilder : ConnectionStringBuilder
    {
        /// <summary>
        ///   The SQL Server ODBC driver name.
        /// </summary>
        public const string DbProviderNameValue = "MSSQLSERVER";


        /// <summary>
        ///   Creates a new instance of the <see cref="Centido.Core.SqlServerConnectionStringBuilder"/> class.
        /// </summary>
        public SqlServerConnectionStringBuilder()
        {
            Driver = "MSSQLSERVER";
        }


        /// <summary>
        ///   Gets the target rdbms product name.
        /// </summary>
        public override string DbProviderName
        {
            get { return DbProviderNameValue; }
        }


        /// <summary>
        ///   Gets the target rdbms product name.
        /// </summary>
        public override string ProductName
        {
            get { return "SQLSERVER"; }
        }


        /// <summary>
        ///   Builds a connection string
        /// </summary>
        /// <returns>Returns the built connection string</returns>
        public override string Build()
        {
            var connectionString =
                string.Format("Driver={{{0}}};Server={1};{2};Uid={3};Pwd={4};",
                    Driver,
                    Host + (PortNumber != 0 ? "," + PortNumber : string.Empty),
                    (!string.IsNullOrEmpty(DbName) ? "Database=" + DbName : string.Empty),
                    UserName,
                    UserPassword);

            return connectionString;
        }


        /// <summary>
        ///   Parses connection string parameters
        /// </summary>
        /// <param name = "connectionString">Indicates the connection string</param>
        public override void Parse(string connectionString)
        {
            if (connectionString == null)
                throw new ArgumentNullException("connectionString");

            // Parsing the driver name
            Driver = ParseDriverName(connectionString) ?? Driver;

            // Parsing the host name
            Host = ParseStringParameter(connectionString, "Server") ?? Host;
            PortNumber = 0;

            // Parsing initial catalog
            DbName = ParseStringParameter(connectionString, "Database") ?? DbName;

            UserName = ParseStringParameter(connectionString, "Uid") ?? UserName;
            UserPassword = (ParseStringParameter(connectionString, "Pwd") ?? UserPassword);
        }


        /// <summary>
        ///   Creates a new connection string builder of the current type.
        /// </summary>
        /// <returns>Returns a new connection string builder that has the same type
        /// as the current one.</returns>
        public override ConnectionStringBuilder CreateNew()
        {
            return new SqlServerConnectionStringBuilder();
        }
    }
}